//
//  HMWelcomeViewController.swift
//  weibo28
//
//  Created by 范英强 on 16/3/25.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import SDWebImage
class HMWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setupHeadImageViewAnim()
    }
    
    // MARK: - 设置headImageView动画
    private func setupHeadImageViewAnim(){
        // 更新head约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        /**
        动画方法
        
        - parameter <Tduration:             动画执行时间
        - parameter delay:                  延迟时间
        - parameter usingSpringWithDamping:  阻尼（0-1）（阻尼越大 弹性效果越差）
        - parameter initialSpringVelocity:  初始速度
        - parameter options:                动画选项
        - parameter animations:
        - parameter completion:
        */
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 页面要重新布局
            self.view.layoutIfNeeded()

            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 发送通知 告知appdelegtate 更改根视图
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVcNotification, object: nil)
                        
                })
                
        }
        
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 01 添加控件
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        // 02 添加约束
        // 01
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(300)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        // 02
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    
    // MARK: - 懒加载控件
    // 背景图片
    private lazy var  bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // 头像
    private lazy var headImageView: UIImageView = {
        let img = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 设置img layer相关属性
        img.layer.cornerRadius = 85 * 0.5
        img.layer.borderColor = themeColor.CGColor
        img.layer.borderWidth = 3
        
        img.clipsToBounds = true
        // 设置网络图片
        img.sd_setImageWithURL(NSURL(string: HMUserAccountTools.sharedTools.getUserAccount()?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return img
        
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGrayColor(), font: 18)
        lab.textAlignment = .Center
        lab.text = "欢迎回来"
        lab.alpha = 0
        return lab
    }()
    
    
    
    

}
